package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.Customer;
import cn.zj.logistics.pojo.CustomerExample;
import cn.zj.logistics.pojo.CustomerExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.BaseDataService;
import cn.zj.logistics.service.CustomerService;
import cn.zj.logistics.service.UserService;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private BaseDataService baseDataService;

	// 跳转到 customer 管理页面
	@RequiresPermissions("customer:customerPage")
	@RequestMapping("/customerPage")
	public String customerPage() {
		return "customerPage";
	}

	@ResponseBody
	@RequiresPermissions("customer:update")
	@RequestMapping("/update")
	public MessageObject update(Customer customer) {

		int row = customerService.updateByPrimaryKeySelective(customer);

		MessageObject mo = MessageObject.creatMO(0, "修改数据失败，请联系管理员");
		if (row == 1) {
			mo = MessageObject.creatMO(1, "修改数据成功");
		}

		return mo;

	}

	@ResponseBody
	@RequiresPermissions("customer:insert")
	@RequestMapping("/insert")
	public MessageObject insert(Customer customer) {

		int row = customerService.insert(customer);

		MessageObject mo = MessageObject.creatMO(0, "新增数据失败，请联系管理员");
		if (row == 1) {
			mo = MessageObject.creatMO(1, "新增数据成功");
		}

		return mo;

	}

	/*
	 * jackson+@ResponseBody 方法直接返回json格式的数据
	 * 
	 */

	@ResponseBody
	@RequiresPermissions("customer:list")
	@RequestMapping("/list")
	public PageInfo<Customer> list(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize, String keyword) {
		PageHelper.startPage(pageNum, pageSize);
		
		/*
		 * 条件：如果当前角色是业务员
		 * 业务员就只能查看到当前业务员自己的客户
		 */
		//1.获取当前认证的身份
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		System.out.println(user);

		CustomerExample example = new CustomerExample();
		
		Criteria criteria1 = example.createCriteria();
		if("业务员".equals(user.getRolename())) {
			
			//2，查询当前自己的客户
			criteria1.andUserIdEqualTo(user.getUserId());
		}

		// 开始条件查询
		if (StringUtils.isNotBlank(keyword)) {

			
			criteria1.andCustomerNameLike("%" + keyword + "%");

		}

		List<Customer> customers = customerService.selectByExample(example);

		PageInfo<Customer> pageInfo = new PageInfo<>(customers);

		return pageInfo;
	}

	@ResponseBody
	@RequiresPermissions("customer:delete")
	@RequestMapping("/delete")
	public MessageObject delete(Long customerId) {

		int row = customerService.deleteByPrimaryKey(customerId);

		MessageObject mo = MessageObject.creatMO(0, "删除数据失败，请联系管理员！！");

		// 删除成功
		if (row == 1) {
			mo = MessageObject.creatMO(1, "删除数据成功！！！");
		}

		return mo;
	}

	/*
	 * 新增或者修改跳转的编辑页面
	 * 
	 */

	@RequestMapping("/customerEdit")
	public String customerEdit(Model m, Long customerId) {
		
		//获取当前认证身份
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		m.addAttribute("user", user);

		/*
		 * 1.查询出所有的业务员
		 *
		 */
		
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRolenameLike("业务员");
		List<User> users = userService.selectByExample(example );
		m.addAttribute("users", users);
		
		// 2.查询出基础数据中所有的区间数据
		List<BaseData> baseDatas = baseDataService.selectBaseDatasByParentName("区间管理");
		m.addAttribute("baseDatas", baseDatas);
		System.out.println(baseDatas);
		/*
		 * 如果是修改，有customerId，要根据id查询出对应的管理员并共享让jsp页面回显
		 */
		if (customerId != null) {
			Customer customer = customerService.selectByPrimaryKey(customerId);
			m.addAttribute("customer", customer);
		}

		return "customerEdit";
	}

}
